package ink.xiaobaibai.controller;

import ink.xiaobaibai.annotation.InsidePublicResources;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.serviceOxy.onePay.IUploadService;
import ink.xiaobaibai.serviceOxy.onePay.impl.QiniuUploadServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @description: 七牛云接口
 * @author: 小白白
 * @create: 2021-07-11
 **/

@Api(tags = "七牛云接口")
@RestController
@RequestMapping("/qiniu")
@CrossOrigin
@InsidePublicResources
public class QiNiuController {

    @Autowired
    private IUploadService qiniuUploadServiceImpl;

    @GetMapping("/image-token")
    @ApiOperation("获取上传照片所需的token")
    public ResponseFormat getImageToken() {
        String qnToken = this.qiniuUploadServiceImpl.getQiniuToken();
        String fileName = UUID.randomUUID().toString().substring(16);//从第十六位开始截取,(36-16)总共截取20个字符
        Map<String, String> map = new HashMap<>();
        map.put("qiniu_url", QiniuUploadServiceImpl.public_httpUrl + fileName);
        map.put("qiniu_token", qnToken);
        map.put("qiniu_key", fileName);
        return ResponseFormat.success(map);
    }

}
